<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2019/5/23
 * Time: 15:03
 */
namespace app\admin\controller;
use app\admin\model\Models;
use app\admin\model\Mokuais;
use app\admin\model\Subject;
use app\admin\model\UpFile;
use think\Controller;
use app\admin\model\Column as Columns;
use think\Request;

class Column extends Controller
{
    /**查看栏目
     * @return mixed
     */
    public function show_list(){
        return NewBuilder::make('table')
            ->addTableHeaderAllDel('column/del')
            ->addTableHeaderAdd('column/show_add')
            ->addTableColumn('id','text','ID')
            ->addTableColumn('name','text','栏目名称',null,null,null,null,'left')
            ->addTableColumn('en_name','text','栏目英文名')
            ->addTableColumn('model','text','模型')
            ->addTableColumn('pid','text','上级栏目')
            ->addTableColumn('imgs','img','图标')
            ->addTableColumn('mokuai_id','text','模块')
            ->addTableColumn('status','switch','是否开启编辑数据',['开启','关闭'],'column/change')
            ->addTableRightHeader('操作',null,null,230)
            ->addTableRight('addchild','column/show_add','添加子栏目')
            ->addTableRight('edit','column/show_edit','修改')
            ->addTableRight('del','column/del','删除')
            ->addTablePage('column/page',null,null,false)
            ->fetch();
    }

    /**加载数据
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function page(Request $request){
        $obj = new Subject();
        $data = $obj->Datas(0,0);
        $str = '-';
        foreach ($data as $key=>$value){
            if($value['cheng']>0){
                $data[$key]['name'] = '|'.str_repeat($str,$value['cheng']*2).$value['name'];
            }
            $models = Models::where('id',$value['model_id'])->findOrEmpty()->toArray();
            if($models){
                $data[$key]['model'] = $models['model_name'];
            }
            if($value['pid']==0){
                $data[$key]['pid'] = '最上级';
            }else{
                $column = Columns::where('id',$value['pid'])->findOrEmpty()->toArray();
                if($column){
                    $data[$key]['pid'] = $column['name'];
                }
            }
            if($value['mokuai_id']){
                $mokuai = Mokuais::where('id',$value['mokuai_id'])->findOrEmpty()->toArray();
                if($mokuai){
                    $data[$key]['mokuai_id'] = $mokuai['name'];
                }
            }
            $data[$key]['imgs'] = getFilePath($value['imgs']);
        }
        $respo['data'] = $data;
        $respo['total'] = count($data);
        return json_page_response($respo);
    }

    /**修改状态
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function change(Request $request){
        $id = $request->param('id');
        $data = Columns::where('id',$id)->findOrEmpty()->toArray();
        if($data){
            if($data['status']==1){
                $data['status'] = 0;
            }else{
                $data['status'] = 1;
            }
            if(!$data['model_id']){
                $data['status'] = 0;
            }
            if(Columns::where('id',$id)->update([
                'status'=>$data['status']
            ])){
                return json_my_response('修改成功',200);
            }else{
                return json_my_response('修改失败');
            }
        }
    }

    /**加载添加栏目
     * @param Request $request
     * @return NewBuilder|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function show_add(Request $request){
        $id = $request->param('id',null);
        $models = Models::where('model_type','单表模型')
            ->where('model_table','<>',session('modular').'_users')
            ->where('status',1)
            ->select()
            ->toArray();
        $model = [];
        foreach ($models as $value){
            $model[$value['id']] = $value['model_name'];
        }
        $lan[0] = '最上级';
        $lanmu = Columns::where('id','>',0)
            ->select()->toArray();
        foreach ($lanmu as $value){
            $lan[$value['id']] = $value['name'];
        }
        $mokuais = Mokuais::where('status',1)
            ->select()->toArray();
        $mokuai = [];
        foreach ($mokuais as $value){
            $mokuai[$value['id']] = $value['name'];
        }
        $dat= NewBuilder::make('form')
            ->addAction('column/add')
            ->addInput('name','栏目名称')
            ->addInput('en_name','英文名称')
            ->addImg('imgs','图标')
            ->addInput('action','后台链接地址','document/show_list')
            ->addSelect('model_id','选择模型',null,$model,true);
        if($id){
            $dat=$dat->addSelect('pid','选择上级栏目',$id,$lan,true);
        }else{
            $dat=$dat->addSelect('pid','选择上级栏目',null,$lan,true);
        }
        $dat=$dat->addInput('url','填写文件所在文件夹')
            ->addInput('list','填写列表页','list')
            ->addInput('detail','填写详情页','detail')
            ->addInput('index_action','前端url')
            ->addSelect('mokuai_id','选择模块',1,$mokuai,true)
            ->addFormButton('submit','提交')
            ->addFormButton('back','返回')
            ->fetch();
        return $dat;
    }

    /**添加栏目
     * @param Request $request
     * @return \think\response\Redirect
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function add(Request $request){
        $data = $request->param();
        $data['status'] = 1;
        $data['create_time'] = time();
        if(!$data['action']){
            $data['action'] = 'document/show_list';
        }
        if(isset($data['model_id'])&&$data['model_id']){
            $data['status'] = 1;
        }else{
            $data['status'] = 0;
        }
        $bool = Columns::create($data);
        if($bool){
            Subject::addLianBiaoJur(['cid'=>$bool->id]);
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }

    /**编辑
     * @param Request $request
     * @return NewBuilder|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function show_edit(Request $request){
        $id = $request->param('id');
        $models = Models::where('model_type','单表模型')
            ->where('model_table','<>',session('modular').'_users')
            ->where('status',1)
            ->select()
            ->toArray();
        $data = Columns::where('id',$id)->findOrEmpty()->toArray();
        $model = [];
        foreach ($models as $value){
            $model[$value['id']] = $value['model_name'];
        }
        $lan[0] = '最上级';
        $lanmu = Columns::where('id','>',0)
            ->select()->toArray();
        foreach ($lanmu as $value){
            $lan[$value['id']] = $value['name'];
        }
        $mokuais = Mokuais::where('status',1)
            ->select()->toArray();
        $mokuai = [];
        foreach ($mokuais as $value){
            $mokuai[$value['id']] = $value['name'];
        }
        $dat= NewBuilder::make('form')
            ->addAction('column/edit')
            ->addNone('id',$data['id'])
            ->addInput('name','栏目名称',$data['name'])
            ->addInput('en_name','英文名称',$data['en_name'])
            ->addImg('imgs','图标',$data['imgs'])
            ->addInput('action','后台链接地址',$data['action'])
            ->addSelect('model_id','选择模型',$data['model_id'],$model,true);
        $dat=$dat->addSelect('pid','选择上级栏目',$data['pid'],$lan,true);
        $dat=$dat->addInput('url','填写文件所在文件夹',$data['url'])
            ->addInput('list','填写列表页','list',$data['list'])
            ->addInput('detail','填写详情页',$data['detail'])
            ->addInput('index_action','前端url',$data['index_action'])
            ->addSelect('mokuai_id','选择模块',$data['mokuai_id'],$mokuai,true)
            ->addFormButton('submit','提交')
            ->addFormButton('back','返回')
            ->fetch();
        return $dat;
    }

    /**编辑
     * @param Request $request
     * @return \think\response\Redirect
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function edit(Request $request){
        $data = $request->param();
//        if(isset($data['status'])&&$data['status']=='on'){
//            $data['status'] = 1;
//        }else{
//            $data['status'] = 0;
//        }
        if(isset($data['model_id'])&&$data['model_id']){
            $data['status'] = 1;
        }else{
            $data['status'] = 0;
        }
        if(Columns::where('id',$data['id'])->update($data)){
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }

    /**删除栏目
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function del(Request $request){
        $id = $request->param('id');
        $data  = Columns::where('id','in',$id)
            ->select();
        Subject::deleteColumnToJur($id);
        if(Columns::where('id','in',$id)->delete()){
            foreach ($data as $value){
                UpFile::delFile($value['imgs']);
            }
            return json_my_response('删除成功',200);
        }else{
            return json_my_response('删除失败');
        }
    }

}